package com.ruoyi.hr.controller.hr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.hr.HrConstants;
import com.ruoyi.hr.domain.Departments;
import com.ruoyi.hr.domain.Empentryinfo;
import com.ruoyi.hr.domain.Jobs;
import com.ruoyi.hr.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/empentry")
public class EmpentryController extends BaseController {
    @Autowired
    private JinPengService jinPengService;

    @Autowired
    private EmpentryinfoService empentryinfoService;

    @Autowired
    private CompanysService companysService;

    @Autowired
    private DepartmentsService departmentsService;

    @Autowired
    private JobsService jobsService;

    private String prefix = "hr/empentryinfo";

    /**
     * 跳转分页页面
     */
    @GetMapping()
    public String user() {
        return prefix + "/empentryinfo";
    }


    /**
     * 获取分页数据以及数据总量
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() {
        int PageNum = getNum(); //取得页数
        int pageSize = getSize(); //取得显示数
        List<Empentryinfo> list = null; //分页List
        int PageCount;//总数据量
        list = empentryinfoService.findAll(PageNum, pageSize);
        PageCount = empentryinfoService.findCount();
        return getDataTable1(list, PageCount);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("companys", companysService.findAllCompanys(null));
        mmap.put("departments", departmentsService.findDep((long)33));
        return prefix + "/add";
    }


    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:emp:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Empentryinfo empentryinfo)
    {
        if (HrConstants.badge_NOT_UNIQUE.equals(jinPengService.checkBadgeUnique(empentryinfo.getBadge())))
        {
            return error("新增员工'" + empentryinfo.getBadge()+ "'失败，员工编号已存在");
        }
        return toAjax(empentryinfoService.insertEmp(empentryinfo));
    }

    /**
     * 根据公司ID获取相应的部门
     * @param compID 公司ID
     */
    @PostMapping("/findDep")
    @ResponseBody
    public List<Departments> findDep(Long compID) {
        return departmentsService.findDep(compID);
    }

    /**
     * 根据部门ID获取相应的岗位
     * @param DepID 公司ID
     */
    @PostMapping("/findJob")
    @ResponseBody
    public List<Jobs> findJob(Long DepID) {
        return jobsService.findJobs(DepID);
    }

    /**
     * 校验员工编号
     */
    @PostMapping("/checkBadgeUnique")
    @ResponseBody
    public String checkBadgeUnique(String badge)
    {
        String a = jinPengService.checkBadgeUnique(badge);
        return a;
    }
}
